package com.example.springtest.exam.controller;


import com.example.springtest.common.R;
import com.example.springtest.exam.pojo.Exam;
import com.example.springtest.exam.service.ExamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("exam")
@Slf4j
public class ExamController {

    @Autowired
    private ExamService examService;

    /**
     * 获取考试
     * @return
     */
    @GetMapping("list")
    public R<List<Exam>> getExamList(){
        List<Exam> examList=examService.getExamList();
        return R.ok(examList);
    }

    /**
     * 增加考试
     * @param exam
     * @return
     */
    @PostMapping("add")
    public R<Void> addExam(@RequestBody @Validated Exam exam){
        log.debug("增加考试信息：{}",exam);
        examService.addExam(exam);
        return R.ok();
    }

    /**
     * 删除考试
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R<Void> deleteExam(@PathVariable Integer id){
        log.debug("id值为；{}",id);
        examService.deleteExam(id);
        return R.ok();
    }

    /**
     * 编辑考试
     * @param id
     * @param exam
     * @return
     */
    @PutMapping("edit/{id}")
    public R<Void> editExam(@PathVariable Integer id,@RequestBody @Validated Exam exam){
        examService.editExam(id,exam);
        return  R.ok();
    }

    /**
     * 更新考试状态
     * @param id
     * @return
     */
    @PatchMapping("updateStatus/{id}")
    public R<Exam> updateStatus(@PathVariable Integer id){
        Exam exam=examService.updateStatus(id);
        return R.ok(exam);
    }
}
